package com.dcits.electronic_account.controller;

import com.dcits.electronic_account.bean.BingBank;
import com.dcits.electronic_account.bean.ResponseJson;
import com.dcits.electronic_account.bean.Flow;
import com.dcits.electronic_account.bean.Withdraw;
import com.dcits.electronic_account.constant.Constant;
import com.dcits.electronic_account.service.impl.WithdrawalImpl;
import com.dcits.electronic_account.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: 薛世坤
 * @Date： 2022/6/26
 */
@RestController
@RequestMapping("/user")
public class WithdrawalController {

    @Autowired
    public WithdrawalImpl withdrawal;

    @Autowired
    public BingBankCardListController bingBankCardList;

    @RequestMapping("/withdrawal")
     public ResponseJson withdrawal(@RequestBody Withdraw withdraw,HttpServletRequest request){
         //HttpSession session=request.getSession();
       // String userid = request.getParameter("userId");
        //拿出存在session中的当前用户
         //virtual_account user = (virtual_account) session.getAttribute("user");
         //校验登录状态
         //int flag = withdrawal.check_login_state(withdraw.getUserId());
             //判断是否绑定银行卡
             List<BingBank> list = withdrawal.checkBingBankCard(withdraw.getUserId());
             if(!list.isEmpty()){
                 //校验账户和密码
                 String name = withdrawal.checkBankCardAndPassword(withdraw.getBank_card_number(), MD5Util.getMd5(withdraw.getPassword()));
                 if(!(name==null)) {
                     //校验金额
                     if (withdraw.getBalance().compareTo(BigDecimal.ZERO) == -1) {
                         return new ResponseJson<>(Constant.FAIL_CODE, "提现余额为负");
                     } else if (withdraw.getBalance().compareTo(BigDecimal.ZERO) == 0) {
                         return new ResponseJson<>(Constant.FAIL_CODE, "提现余额为0");
                     }else {
                         int number = withdrawal.check_balance(withdraw.getBank_card_number(), withdraw.getBalance());
                         if (number == 1) {
                             int wit = this.withdrawal.withdrawal(withdraw.getBank_card_number(), withdraw.getBalance(), withdraw.getUserId());
                             if (wit > 0) {
                                 //public flow(String time, Integer type, double amount, Integer payer, Integer payee) {
                                 Date time = new Date(System.currentTimeMillis());
                                 DateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                 withdrawal.insert_flow(new Flow(withdrawal.select_fid(), sbf.format(time), 1, withdraw.getBalance(), withdraw.getUserId(), withdraw.getBank_card_number()));
                                 return new ResponseJson<>(Constant.SUCCESS_CODE, "提现成功");
                             }
                         } else {
                             return new ResponseJson<>(Constant.FAIL_CODE, "余额不足");
                         }
                     }
                 }else {
                         return new ResponseJson<>(Constant.FAIL_CODE, "银行卡或密码错误");
                 }
             }else{
                 return new ResponseJson<>(Constant.FAIL_CODE,"未绑定银行卡");
             }
        return new ResponseJson<>(Constant.FAIL_CODE,"错误");
     }
}
